﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Move_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["pro_num"] = Request.QueryString["id"];
        Session["UserId"] = "";
        //FLogin.Attributes.Add("Value", "用户名");                                                       //用户名提示语点击后取消
        //FLogin.Attributes.Add("OnFocus", "if(this.value=='用户名') {this.value=''}");
        //FLogin.Attributes.Add("OnBlur", "if(this.value==''){this.value='用户名'}");

        //FPass.Attributes.Add("Value", "密码");                                                    //密码提示语点击后取消
        //FPass.Attributes.Add("OnFocus", "if(this.value=='密码'){this.value=''}");
        //FPass.Attributes.Add("OnBlur", "if(this.value==''){this.value='密码'}");

    }

    //protected void IBtLogin_Click(object sender, EventArgs e)
    //{
    //    //GridView1.DataBind();
    //    //SqlDataSource1.DataBind();
    //    //int r = GridView1.PageCount;
    //    //if (r == 1)
    //    //{
    //    //        Session["UserId"] = FLogin.Text.Trim().ToString();
    //    //        Response.Redirect("Move_Rep.aspx");  
    //    //}
    //    //else
    //    //{
    //    //    Label1.Text = "<p><font color='red'>用户名或密码错误</font></p>";
    //    //}
    //}

    //protected void ButDirect_Click(object sender, EventArgs e)
    //{
    //    //Response.Redirect("Move_User.aspx");
    //}


    protected void Sign_Up_Click(object sender, EventArgs e)
    {
        Response.Redirect("Move_User_Signup.aspx");
    }

    protected void Login_Click(object sender, EventArgs e)
    {
        string SignUpName = FLogin.Text.Trim();
        string PassWord = FPass.Text.Trim();
        int UserType = Convert.ToInt32(DropDownList1.SelectedValue);
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            string SQL = "select uername from users where uername=@SignUpName and password=@PassWord and usertype=@UserType and flag=1";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = SQL;

            //依次添加每个字段的值
            SqlParameter pSignUpName = new SqlParameter("@SignUpName", SqlDbType.VarChar, 11);
            pSignUpName.Value = SignUpName;
            cmd.Parameters.Add(pSignUpName);
            SqlParameter pPassWord = new SqlParameter("@PassWord", SqlDbType.VarChar, 20);
            pPassWord.Value = PassWord;
            cmd.Parameters.Add(pPassWord);
            SqlParameter pUserType = new SqlParameter("@UserType", SqlDbType.Int);
            pUserType.Value = UserType;
            cmd.Parameters.Add(pUserType);
            int flag = 0;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    ++flag;
                }
            }

            //int R = cmd.ExecuteNonQuery();  //R 的值就是返回影响表的行数
            if (flag > 0)
            {
                Session["Move_Name"] = SignUpName;
                Session["Move_type"] = UserType;
                if (UserType==0)
                {
                    //Session["pro_num"] = SignUpName;
                    Session["Move_Islogin"] = 1;
                    Response.Redirect("Move_Rep.aspx");
                }
                else
                {
                    Session["Move_Islogin"] = 2;
                    Response.Redirect("Move_User.aspx");
                }
            }
            else
            {
                Label1.Text = "用户名或密码错误，请重试！"; 
                
            }
            //Session["ReturnPath"] = "Admin_Add.aspx";
            //Response.Redirect("Mid.aspx");
        }
        finally
        {
            conn.Close();
        }
    }

    protected void FaceLogin_Click(object sender, EventArgs e)
    {
        Response.Redirect("User_Face_Login.aspx");
    }
}